package com.ruoyi.xiaoshou.service.impl;

import com.ruoyi.IndexDtoByThree;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.xiaoshou.domain.SaleOrder;
import com.ruoyi.xiaoshou.domain.SaleOrderDetail;
import com.ruoyi.xiaoshou.domain.SaleRequest;
import com.ruoyi.xiaoshou.domain.SaleRequestDetail;
import com.ruoyi.xiaoshou.dto.IndexSaleDto;
import com.ruoyi.xiaoshou.dto.UpdateOrderDto;
import com.ruoyi.xiaoshou.mapper.SaleOrderDetailMapper;
import com.ruoyi.xiaoshou.mapper.SaleOrderMapper;
import com.ruoyi.xiaoshou.mapper.SaleRequestDetailMapper;
import com.ruoyi.xiaoshou.mapper.SaleRequestMapper;
import com.ruoyi.xiaoshou.service.ISaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 销售订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
@Transactional
public class SaleOrderServiceImpl implements ISaleOrderService {
    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Autowired
    private SaleRequestMapper saleRequestMapper;

    @Autowired
    private SaleRequestDetailMapper saleRequestDetailMapper;

    @Autowired
    private SaleOrderDetailMapper saleOrderDetailMapper;


    /**
     * 查询销售订单
     *
     * @param Id 销售订单主键
     * @return 销售订单
     */
    @Override
    public SaleOrder selectSaleOrderById(Integer Id) {
        return saleOrderMapper.selectSaleOrderById(Id);
    }

    @Override
    public SaleOrder getOrderByCode(String orderCode) {
        SaleOrder saleOrder = saleOrderMapper.getOrderByCode(orderCode);
        return saleOrder;
    }

    @Override
    public IndexSaleDto getSaleOrderCount() {
        return saleOrderMapper.getSaleOrderCount();
    }

    @Override
    public int updateSaleOrderStatus(UpdateOrderDto updateOrderDto) {
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setOrderStatus(updateOrderDto.getApprovalResult());
        saleOrder.setRemark(updateOrderDto.getApprovalOption());
        saleOrder.setCode(updateOrderDto.getCode());
        saleOrder.setAuditName(SecurityUtils.getUsername());
        int num  = saleOrderMapper.updateSaleOrder(saleOrder);
        return num;
    }

    @Override
    public IndexDtoByThree getThreeCountToIndex() {
        return saleOrderMapper.getThreeCountToIndex();
    }

    /**
     * 查询销售订单列表
     *
     * @param saleOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<SaleOrder> selectSaleOrderList(SaleOrder saleOrder) {
        return saleOrderMapper.selectSaleOrderList(saleOrder);
    }

    @Override
    public List<SaleOrder> noReconList(SaleOrder saleOrder) {
        return saleOrderMapper.noReconList(saleOrder);
    }

    /**
     * 新增销售订单
     *
     * @param
     * @return 结果
     */
    @Override
    public String insertSaleOrder(int id) {
        SaleRequest saleRequest = saleRequestMapper.selectSaleRequestById(id);
        SaleOrder saleOrder = new SaleOrder();
        List<SaleRequestDetail> saleBackRequestDetailList = saleRequestDetailMapper.getList(id);
        BigDecimal total = BigDecimal.valueOf(0);
        int outStockNum = 0;
        for (SaleRequestDetail saleRequestDetail : saleBackRequestDetailList) {
            total = saleRequestDetail.getSubtotal().add(total);
            outStockNum +=saleRequestDetail.getCount();
        }
        saleOrder.setCode("XXDD" + System.currentTimeMillis());
        saleOrder.setCusName(saleRequest.getCusName());
        saleOrder.setCreateBy(SecurityUtils.getUsername());
        saleOrder.setAuditName(saleRequest.getAuditName());
        saleOrder.setCostPrice(saleRequest.getCostPrice());
        saleOrder.setTotal(total);
        saleOrder.setCusId(saleRequest.getCusId());
        saleOrder.setRequestOrderCode(saleRequest.getCode());
        saleOrder.setOutStockNum(outStockNum);
        //添加销售订单
        int count = saleOrderMapper.insertSaleOrder(saleOrder);
        if (count < 0) {
            throw new RuntimeException("系统错误，添加失败");
        }
        //通过订单编号获取销售订单id
        int orderId = saleOrderMapper.getOrderId(saleOrder.getCode());
        for (SaleRequestDetail saleRequestDetail : saleBackRequestDetailList) {
            SaleOrderDetail saleOrderDetail = new SaleOrderDetail();
            saleOrderDetail.setCostPrice(saleRequestDetail.getCostPrice());
            saleOrderDetail.setOrderId(orderId);
            saleOrderDetail.setNumber(saleRequestDetail.getCount());
            saleOrderDetail.setProId(saleRequestDetail.getProId());
            saleOrderDetail.setPrice(saleRequestDetail.getPrice());
            saleOrderDetail.setCreateBy(SecurityUtils.getUsername());
            int count1 = saleOrderDetailMapper.addOrderDetail(saleOrderDetail);
            if (count1 < 0) {
                throw new RuntimeException("系统错误，添加失败");
            }
        }
        saleRequest.setStatus(3);
        int count2 = saleRequestMapper.updOrder(saleRequest);
        if (count2 < 0) {
            throw new RuntimeException("系统错误，添加失败");
        }
        return "添加成功";
    }

    /**
     * 修改销售订单
     *
     * @param saleOrder 销售订单
     * @return 结果
     */
    @Override
    public int updateSaleOrder(SaleOrder saleOrder) {
        return saleOrderMapper.updateSaleOrder(saleOrder);
    }

    /**
     * 批量删除销售订单
     *
     * @param Ids 需要删除的销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSaleOrderByIds(Integer[] Ids) {
        return saleOrderMapper.deleteSaleOrderByIds(Ids);
    }

    @Override
    public List<SaleOrder> getAllList() {
        return saleOrderMapper.getAllList();
    }

    /**
     * 删除销售订单信息
     *
     * @param Id 销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSaleOrderById(Integer Id) {
        return saleOrderMapper.deleteSaleOrderById(Id);
    }

    /**
     * @return java.util.List<com.ruoyi.xiaoshou.domain.SaleOrder>
     * @description 根据编号集合获取销售订单集合
     * @date 2024/11/12 上午9:17
     * @Param orderCodes:
     **/
    public List<SaleOrder> getSaleOrderListByOrderCodes(String[] codes) {
        List<SaleOrder> saleOrderList = saleOrderMapper.selectSaleOrderListByOrderCodes(codes);
        return saleOrderList;
    }

    /**
     * @return java.util.List<com.ruoyi.xiaoshou.domain.SaleOrder>
     * @description 用于销售收款单的查询销售订单
     * @date 2024/11/12 上午10:17
     * @Param saleOrder:
     **/
    public List<SaleOrder> selectSaleOrderListForReciveOrder(SaleOrder saleOrder) {
        return saleOrderMapper.selectSaleOrderListForReciveOrder(saleOrder);
    }
}
